package com.mfh.auto.create.mvvm.autocreatemvvm.file.hm

import com.intellij.psi.PsiDirectory
import com.mfh.auto.create.mvvm.autocreatemvvm.Config
import com.mfh.auto.create.mvvm.autocreatemvvm.file.BaseFileModel
import com.mfh.auto.create.mvvm.autocreatemvvm.file.hm.ConstantEdit
import com.mfh.auto.create.mvvm.autocreatemvvm.utils.CommonUtils

/**
 * 鸿蒙页面
 */
object HMPageFileModel : BaseFileModel() {

    override fun buildFileName() = "${Config.name}Page.ets"

    override fun buildContentString(directory: PsiDirectory): String {
        val name = Config.name
        val routeKey = CommonUtils.camelToUnderscore(name).uppercase()

        ConstantEdit.addConstant(
            directory,
            "  // ${Config.desc}\n" +
                    "  public static readonly $routeKey = '${routeKey.lowercase()}'"
        )

        return buildString {
            appendLine(
                "import { Route, ZRouter } from \"@hzw/zrouter\";\n" +
                        "import { RouterPathConstant } from \"library\";\n" +
                        "import { ${Config.name}ViewModel } from \"./${Config.name}ViewModel\";\n" +
                        "\n" +
                        "/**\n" +
                        " * @desc: ${Config.desc}\n" +
                        " */\n" +
                        "@ComponentV2\n" +
                        "@Route({ name: RouterPathConstant.$routeKey })\n" +
                        "export struct ${name}Page {\n" +
                        "  // 启动方法\n" +
                        "  static start() {\n" +
                        "    ZRouter.getInstance().push(RouterPathConstant.$routeKey)\n" +
                        "  }\n" +
                        "\n" +
                        "  @Local\n" +
                        "  viewModel: ${name}ViewModel = new ${name}ViewModel()\n" +
                        "\n" +
                        "  build() {\n" +
                        "    NavDestination() {\n" +
                        "      Column() {\n" +
                        "        \n" +
                        "      }\n" +
                        "    }\n" +
                        "    .hideTitleBar(true)\n" +
                        "    .onAppear(() => {\n" +
                        "      let vm = this.viewModel\n" +
                        "      this.viewModel.self = vm\n" +
                        "      this.viewModel.onAppear()\n" +
                        "    })\n" +
                        "    .onShown(() => this.viewModel.onShow())\n" +
                        "    .onHidden(() => this.viewModel.onHide())\n" +
                        "    .onDisAppear(() => this.viewModel.onDisappear())\n" +
                        "  }\n" +
                        "}"
            )
        }
    }
}